
layui.define(['table', 'form'], function(exports){
  var $ = layui.$
  ,table = layui.table
  ,form = layui.form;

  //用户管理
  table.render({
    elem: '#LAY-user-back-manage'
    ,url: layui.setter.html + '/api/leaves' //模拟接口
    ,cols: [[
       {field: 'bizTag', title: '业务标签'}
      ,{field: 'maxId', title: 'max_id'}
      ,{field: 'step', title: '步长'}
      ,{field: 'description', title: '描述'}
      ,{field: 'updateTime', title: '修改时间'}
      ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-leaf-update'}
    ]]
    ,text: '对不起，加载出现异常！'
  });
  
  //监听工具条
  table.on('tool(LAY-user-back-manage)', function(obj){
    var data = obj.data;
    var bizTag = data.bizTag;
    if(obj.event === 'edit'){
      var tr = $(obj.tr);

      layer.open({
        type: 2
        ,title: '修改描述'
        ,content: '../../../views/home/leaf/update.html'
        ,area: ['420px', '420px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submitID = 'LAY-user-back-submit'
          ,submit = layero.find('iframe').contents().find('#'+ submitID);

          //监听提交
          iframeWindow.layui.form.on('submit(LAY-user-front-submit)', function(data){
            var field = data.field; //获取提交的字段

            //提交 Ajax 成功后，静态更新表格中的数据
            $.ajax({
              url: "/api/leaf",
              type: "PUT",
              dataType: "JSON",
              data: JSON.stringify(field),
              contentType: 'application/json',
              success: function (obj) {
                layer.msg("修改成功")
                table.reload('LAY-user-back-manage'); //数据刷新
                layer.close(index); //关闭弹层
              },
              error: function (obj) {
                layer.msg(obj.responseJSON.message)
              }
            });
            layer.close(index); //关闭弹层
          });  
          
          submit.trigger('click');
        }
        ,success: function(layero, index){
          var body = layer.getChildFrame('body', index);
          var iframeWin = window[layero.find('iframe')[0]['bizTag']]; //得到iframe页的窗口对象，执行iframe页的方法:iframeWin.method();
          //console.log(body.html()) //得到iframe页的body内容
          body.find('#bizTag').val(bizTag)
        }
      })
    }
  });

  exports('leaf', {})
});